﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LeadVinaglue.Helpers
{
    public partial class Paginate
    {
        public static string ResultText(int count)
        {
            string divHTML = string.Empty;

            if (count > 0)
            {
                divHTML = string.Format("Có {0} kết quả", count);
            }

            return divHTML;
        }
        public static string Showing(int take, int pageCurrent, int count)
        {
            string divHTML = string.Empty;

            if (count > 0)
            {
                if (pageCurrent * take >= count)
                    divHTML = divHTML + string.Format("Kết quả <span class=\"fred\">{0}-{1}</span> trong <span class=\"fred\">{2}</span>", (pageCurrent - 1) * take + 1, count, count);
                else
                    divHTML = divHTML + string.Format("Kết quả <span class=\"fred\">{0}-{1}</span> trong <span class=\"fred\">{2}</span>", (pageCurrent - 1) * take + 1, pageCurrent * take, count);
            }

            return divHTML;
        }

        public static string Pagination(string url, int take, int pageCurrent, int count)
        {
            string divHTML = string.Empty;
            string divFirst = string.Empty;
            string divLast = string.Empty;
            string divNext = string.Empty;
            string divPrev = string.Empty;
            int pageSize = 0;
            if (count > 0)
            {
                if (count % take == 0)
                    pageSize = count / take;
                else
                    pageSize = count / take + 1;

                if (pageSize > 1)
                {
                    //first last
                    if (pageCurrent + 2 < pageSize)
                    {
                        divLast = "<li class='btn_pre'><a href='" + string.Format(url, pageSize) + "'>Cuối</a></li>";
                    }
                    if (pageCurrent - 2 > 1)
                    {
                        divFirst = "<li class='btn_pre'><a href='" + string.Format(url, 1) + "'>Đầu</a></li>";
                    }
                    if (pageCurrent > 1)
                    {
                        divPrev = "<li class='btn_pre'><a href='" + string.Format(url, pageCurrent - 1) + "'>Trước</a></li>";
                    }
                  
                    if (pageCurrent < pageSize)
                    {
                        divNext = "<li class='btn_pre'><a href='" + string.Format(url, pageCurrent + 1) + "'>Sau</a></li>";
                    }
                   
                    //paginate
                    if (pageSize <= 5)
                    {
                        for (int i = 1; i <= pageSize; i++)
                        {
                            if (i == pageCurrent)
                                divHTML = divHTML + "<li><a class='active' href='javascript:void(0)'>" + i + "</a></li>";
                            else
                                divHTML = divHTML + "<li class='btn_pre'><a href='" + string.Format(url, i) + "'>" + i + "</a></li>";
                        }
                    }
                    else
                    {
                        int maxPage = pageSize - pageCurrent > 2 ? (pageCurrent - 2 > 0 ? pageCurrent + 2 : (pageSize - pageCurrent >= 5 ? 5 : pageSize)) : pageSize;
                        int minPage = pageCurrent - 2 > 0 ? pageCurrent - 2 : 1;
                        for (int i = minPage; i <= maxPage; i++)
                        {
                            if (i == pageCurrent)
                                divHTML = divHTML + "<li><a class='active' href='javascript:void(0)'>" + i + "</a></li>";
                            else
                                divHTML = divHTML + "<li class='btn_pre'><a href='" + string.Format(url, i) + "'>" + i + "</a></li>";
                        }
                    }
                }
            }

            return divFirst + divPrev + divHTML + divNext + divLast;
        }
    }
}